<template>
    <section>

        <!--列表-->
        <el-table :data="type" highlight-current-row v-loading="listLoading"style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60" label="序号">
            </el-table-column>
            <el-table-column prop="name" label="数据字典名">
                <template scope="scope">
                    <el-button type="text" @click="table = true" >{{ scope.row.name }}</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="sn" label="编号" >
            </el-table-column>
            <el-table-column align='center' width="150" >
                <template slot="header" slot-scope="scope">
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </template>
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--分页工具条-->
        <el-col :span="24" class="toolbar">
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增/编辑界面-->
        <el-dialog :title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="90px" :rules="rules" ref="ruleForm">
                <el-form-item label="字典名" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="字典编号" prop="sn">
                    <el-input v-model="addForm.sn"  auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" >提交</el-button>
            </div>
        </el-dialog>
        <!--抽屉模块-->
        <el-drawer
                title="产品明细表"
                :visible.sync="table"
                direction="rtl"
                size="50%">
            <el-table :data="gridData">
                <el-table-column type="index" width="60" label="序号">
                </el-table-column>
                <el-table-column prop="name" label="明细名">
                </el-table-column>
                <el-table-column align='center' width="150" >
                    <template slot="header" slot-scope="scope">
                        <el-button type="primary" @click="handleAdd">新增</el-button>
                    </template>
                    <template scope="scope">
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-drawer>
        </section>
</template>



<script>
    import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

    export default {
        data() {
            return {
                //产品明细数据
                gridData:[],
                //是否显示抽屉
                table:false,
                //明细表格
                detail:[],
                //校验规则
                rules:{
                    name:[
                        { required: true, message: '数据字典名必填', trigger: 'blur' }
                    ],
                    sn:[
                        { required: true, message: '编号必填', trigger: 'blur' }
                    ]
                },
                //新增/编辑的form表单
                addForm:{
                    //主键id
                    id:"",
                    //部门名
                    name:"",
                    //部门编号
                    sn:"",
                },
                //新增和编辑模态框默认为不弹出
                addFormVisible:false,
                //新增和编辑标题
                title:"",
                //是否加载
                listLoading:false,
                //列表展示
                type:[],
                //每页条数
                pageSize:5,
                //当前页
                currentPage:1,
                //总条数
                total:0
            }
        },
        methods: {
                //给产品名
            show(index,row){

            },
            //给新增和编辑模态框的提交按钮注册事件
            addSubmit(){
                //校验addForm表单
                this.$refs["ruleForm"].validate((valid) => {
                        if (valid) {//校验成功
                            this.$http.post("/type",this.addForm).then(res=>{
                                let{success,msg}=res.data;
                                if(success){
                                    this.$message({
                                        type: success,
                                        message: '添加成功'
                                    });
                                    //关闭模态框
                                    this.addFormVisible=false;
                                    //添加成功，重新加载列表
                                    this.loadTypes();
                                }else{//打印错误
                                    this.$message.error(msg);
                                }
                            })
                        }
                });
            },
            //给新增按钮绑定事件
            handleAdd(){
                //给新增标题赋值
                this.title="新增";
                //重置表单校验属性
                if(this.$refs["ruleForm"]){
                    this.$refs["ruleForm"].resetFields();
                }
                //重置普通属性
                for(var i in this.addForm){
                    this.addForm[i]="";
                }
                //弹出新增模态框
                this.addFormVisible=true;

            },
            //页码改变时，执行此函数
            handleCurrentChange(val){
                this.currentPage=val
                //继续分页查询
                this.loadTypes();
            },

           //给编辑按钮添加事件
            handleEdit(index,row){
                /*console.debug(row);*/
                //给标题设置值
                this.title="编辑"
                //回显普通数据
                for(var i in this.addForm){
                    this.addForm[i]=row[i]
                };
                //弹出模态框
                this.addFormVisible=true;
            },
            //给删除按钮添加事件
            handleDel(index,row){
                console.debug(row)
                /*console.debug(index,row)*/
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {//点击确定执行里面的代码
                    this.$http.delete("/type/"+row.id).then(res=>{
                        //回调函数和解构表达式
                        let {success,msg}=res.data
                        if(success){
                            this.$message({
                                type: success,
                                message: '删除成功'
                            });
                            //删除成功，重新加载列表
                            this.loadTypes();
                        }else{//打印错误
                            this.$message.error(msg);
                        }
                    })
                }).catch(() => {//点击取消执行里面代码
                });
            },
            //加载部门列表
            loadTypes(){
                //显示加载
                this.listLoading=true;
                let parm={
                    "currentPage":this.currentPage,
                    "pageSize":this.pageSize,
                }
                this.$http.patch("/type",parm).then(res=>{
                    //关闭加载
                    this.listLoading=false;
                    /*console.debug(res)*/
                    let{total,list}=res.data
                    this.total=total,
                    this.type=list
                })
            }
        },
        mounted() {
            //加载分页列表
            this.loadTypes();
        }
    }
</script>